package com.tuotu.gps.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.Log;

import com.tuotu.gps.entity.LocationInfo;
import com.tuotu.gps.entity.MessageEvent;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

/**
 * Created by XUYEWEN on 2017/7/23.
 */

public class CollectionUploadPathFile implements   Runnable {
    private Context mContext;
    private String userId;
    Vector<File> pathList=new Vector<>();
    String path= Environment.getExternalStorageDirectory().getPath()+"/gps";
    Thread mThread;
    private SharedPreferences userPreferences;
    HttpNetworkUtil mHttpNetworkUtil;
    private int allSize;
    private int uploadSize;

    public CollectionUploadPathFile(Context context, String userId) {
        this.mContext=context;
        this.userId=userId;
        mThread=new Thread(this);
        mThread.start();
        mHttpNetworkUtil=new HttpNetworkUtil();
    }

    public List<File> getPathList() {
        return pathList;
    }

    @Override
    public void run() {
        userPreferences=mContext.getSharedPreferences(Preference.User.PREFERENCE_USER,Context.MODE_PRIVATE);
        userPreferences.edit().putBoolean(Preference.User.PREFERENCE_UPLOAD_STATUS,true);

        if(null!=path)
            queryFile(path);

        uploadFile();
    }

    private void uploadFile(){
        Iterator<File> iterator2=pathList.iterator();
        Log.e("xyww","CollectionUploadPathFile  uploadFile===="+pathList.size());
        while (iterator2.hasNext()){
            mHttpNetworkUtil.httpPostPathFile(mContext,userId,iterator2.next());
        }
    }

    public void queryFile(String path){
        File file = new File(path);
        if (file.length()>0){
            File[] files = file.listFiles();
            if(null==files){
                return;
            }
            for (File file1:files) {
                if (file1.isDirectory()){
                    queryFile(file1.getPath());
                }else{

                    String tmpName = file1.getPath();//获取全名,如fire.mp3
                    int index = tmpName.lastIndexOf(".");//"."最后出现的位置
                    String ext = tmpName.substring(index+1).toLowerCase();//文件类型名s获取
                    String name=file1.getPath().substring(0,index);
                    if (file1.length() <= 0) {
                        Utils.deleteFile(name);
                        continue;
                    }

                    if(file1.getName().contains("-") && ext.contains("txt")){
                        pathList.add(file1);
                    }
                }

            }
        }
        return ;
    }


    private void sortList(){
        Collections.sort(pathList, new Comparator<File>() {
            @Override
            public int compare(File file, File t1) {
                return file.getName().compareTo(t1.getName());
            }
        });
    }
}
